package com.cskaoyan.gateway.controller.order;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseDataAndTotal;
import com.mall.commons.result.ResponseUtil;
import com.mall.order.OrderCoreService;
import com.mall.order.OrderQueryService;
import com.mall.order.constant.OrderRetCode;
import com.mall.order.dto.*;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.intercepter.TokenIntercepter;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;


/**
 * @Classname OrderController
 * @Description
 * @Date 2021/8/24 20:42
 * @Author terrance_swn
 */
@RestController
@RequestMapping("shopping")
public class OrderController {

    @Reference(timeout = 20000, check = false)
    private OrderQueryService orderQueryService;

    @Reference(timeout = 20000, check = false)
    private OrderCoreService orderCoreService;

    /**
     * 创建订单
     *
     * @param request
     * @param servletRequest
     * @return
     */
    @PostMapping("order")
    public ResponseData createOrder(@RequestBody CreateOrderRequest request, HttpServletRequest servletRequest) {

        //从TokenInterceptor设置的request域中取出当前登录的userId
        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        //Json字符串转换为Json对象
        JSONObject object = JSON.parseObject(userInfo);
        long userId = Long.parseLong(object.get("uid").toString());

        //封装request对象
        request.setUserId(userId);
        request.setUniqueKey(UUID.randomUUID().toString());

        //远程调用
        CreateOrderResponse createOrderResponse = orderCoreService.createOrder(request);

        if (createOrderResponse.getCode().equals(OrderRetCode.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(createOrderResponse.getOrderId());
        }

        return new ResponseUtil<>().setErrorMsg(createOrderResponse.getMsg());
    }

    /**
     * 获取当前用户的全部订单信息
     * @param request
     * @return
     */
    @GetMapping("order")
    public ResponseData getUserAllOrders(OrderListRequest orderListRequest, HttpServletRequest request){

        //从TokenInterceptor设置的request域中取出当前登录的userId
        String userInfo = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        //Json字符串转换为Json对象
        JSONObject object = JSON.parseObject(userInfo);
        long userId = Long.parseLong(object.get("uid").toString());

        //封装请求参数至request
        orderListRequest.setUserId(userId);

        //进行远程调用
        OrderListResponse response = orderQueryService.getUserAllOrders(orderListRequest);

        if(ShoppingRetCode.SUCCESS.getCode().equals (response.getCode())){

            ResponseData responseData = new ResponseUtil().setData(new ResponseDataAndTotal(response.getTotal(), response.getDetailInfoList()));
            return responseData;

        }

        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    /**
     * 获取订单详情
     *
     * @param orderId
     * @param servletRequest
     * @return
     */
    @GetMapping("order/{orderId}")
    public ResponseData getOrderInfo(@PathVariable("orderId") String orderId, HttpServletRequest servletRequest) {

        //从TokenInterceptor设置的request域中取出当前登录的userId
        String userInfo = (String) servletRequest.getAttribute(TokenIntercepter.USER_INFO_KEY);
        //Json字符串转换为Json对象
        JSONObject object = JSON.parseObject(userInfo);
        long userId = Long.parseLong(object.get("uid").toString());

        //封装request
        OrderGoodsRequest request = new OrderGoodsRequest();
        request.setOrderId(orderId);
        request.setUserId(userId);

        //远程调用
        OrderGoodsResponse response = orderQueryService.getOrderDetail(request);
        if(ShoppingRetCode.SUCCESS.getCode().equals (response.getCode())){
            return new ResponseUtil().setData(response.getOrderGoods());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    /**
     * 取消订单
     * @param
     * @return
     */
    @PostMapping("/cancelOrder")
    @ApiOperation("取消订单")
    public ResponseData cancelOrder(@RequestBody CancelOrderRequest cancelOrderRequest) {

        cancelOrderRequest.requestCheck();

        CancelOrderResponse cancelOrderResponse = orderCoreService.cancelOrder(cancelOrderRequest);

        if (OrderRetCode.SUCCESS.getCode().equals(cancelOrderResponse.getCode())) {
            return new ResponseUtil().setData("成功");
        }

        return new ResponseUtil().setErrorMsg(cancelOrderResponse.getMsg());
    }

    /**
     * 删除订单
     * @param orderId
     * @return
     */
    @DeleteMapping("order/{orderId}")
    public ResponseData deleteOrder(@PathVariable("orderId") String orderId) {

        //封装request
        DeleteOrderRequest request = new DeleteOrderRequest();
        request.setOrderId(orderId);

        //远程调用
        DeleteOrderResponse deleteOrderResponse = orderCoreService.deleteOrder(request);

        //结果判断
        if (deleteOrderResponse.getCode().equals(OrderRetCode.SUCCESS.getCode())) {

            return new ResponseUtil<>().setData("成功", "success");
        }

        return new ResponseUtil<>().setErrorMsg(deleteOrderResponse.getMsg());
    }
}



